package com.mysmartphone.views;

import java.util.ArrayList;

import android.app.Fragment;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.mysmartphone.tools.Tools;
import com.mysmartphone.vistas.R;
import com.mysmartphone.vo.Phone;

public class PhoneCompareFragment extends Fragment implements
		OnItemSelectedListener, OnClickListener {

	View rootView;

	Spinner spBrand1;
	Spinner spBrand2;
	Spinner spBrand3;

	Spinner spModel1;
	Spinner spModel2;
	Spinner spModel3;

	Button btSend;
	Button btClean;
	Context context;

	ImageView image1;
	ImageView image2;
	ImageView image3;
	
	TableLayout tableLayout;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		rootView = inflater.inflate(R.layout.phone_compare, container, false);
		context = rootView.getContext();

		tableLayout = (TableLayout) rootView.findViewById(R.id.contentTable);
		
		image1 = (ImageView) rootView.findViewById(R.id.image1);
		image2 = (ImageView) rootView.findViewById(R.id.image2);
		image3 = (ImageView) rootView.findViewById(R.id.image3);

		// Cursor
		ArrayList<String> brands = new ArrayList<String>();
		brands.add(getString(R.string.select));
		brands.add("Nokia");
		brands.add("HTC");
		brands.add("Lenovo");
		brands.add("Zopo");
		brands.add("Samsung");
		brands.add("Cubot");
		brands.add("aaaaaaaaaaaaaaaCubot");
		// Creamos el adaptador

		ArrayAdapter<String> adapter_brand = new ArrayAdapter<String>(
				rootView.getContext(), android.R.layout.simple_spinner_item,
				brands);
		
		//SpinnerListAdapter adapter_brand = new SpinnerListAdapter(rootView.getContext(),brands);

		spBrand1 = (Spinner) rootView.findViewById(R.id.brand1);
		spBrand1.setAdapter(adapter_brand);
		spBrand1.setOnItemSelectedListener(this);

		spBrand2 = (Spinner) rootView.findViewById(R.id.brand2);
		spBrand2.setAdapter(adapter_brand);
		spBrand2.setOnItemSelectedListener(this);
		spBrand2.setEnabled(false);

		spBrand3 = (Spinner) rootView.findViewById(R.id.brand3);
		spBrand3.setAdapter(adapter_brand);
		spBrand3.setOnItemSelectedListener(this);
		spBrand3.setEnabled(false);

		ArrayList<String> model = new ArrayList<String>();

		model.add(getString(R.string.select));

		ArrayAdapter<String> adapter_model = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item, model);

		spModel1 = (Spinner) rootView.findViewById(R.id.model1);
		spModel1.setEnabled(false);
		spModel1.setAdapter(adapter_model);
		spModel1.setOnItemSelectedListener(this);

		spModel2 = (Spinner) rootView.findViewById(R.id.model2);
		spModel2.setEnabled(false);
		spModel2.setAdapter(adapter_model);
		spModel2.setOnItemSelectedListener(this);

		spModel3 = (Spinner) rootView.findViewById(R.id.model3);
		spModel3.setEnabled(false);
		spModel3.setAdapter(adapter_model);
		spModel3.setOnItemSelectedListener(this);

		btSend = (Button) rootView.findViewById(R.id.btSend);
		btSend.setOnClickListener(this);
		btClean = (Button) rootView.findViewById(R.id.btClean);
		btClean.setOnClickListener(this);
		createTable(getListPhone());
		return rootView;
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View v, int position,
			long id) {

		switch (parent.getId()) {
		case R.id.brand1:
			if (position != 0) {
				ArrayList<String> model = new ArrayList<String>();

				model.add(getString(R.string.select));
				model.add("model 1");
				model.add("model 1");
				model.add("model 1");
				model.add("model 1");

				ArrayAdapter<String> adapter_model = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, model);
				
				
				spModel1.setAdapter(adapter_model);
				spModel1.setEnabled(true);
			} else {
				spModel1.setEnabled(false);
				spModel1.setSelection(0);
				image1.setImageResource(R.drawable.phone);
			}
			break;

		case R.id.brand2:
			if (position != 0) {

				ArrayList<String> model = new ArrayList<String>();

				model.add(getString(R.string.select));
				model.add("model 1");
				model.add("model 1");
				model.add("model 1");
				model.add("model 1");

				ArrayAdapter<String> adapter_model = new ArrayAdapter<String>(
						context, android.R.layout.simple_spinner_item, model);
				spModel2.setAdapter(adapter_model);
				spModel2.setEnabled(true);
			} else {
				spModel2.setEnabled(false);
				spModel2.setSelection(0);
				image2.setImageResource(R.drawable.phone);
			}
			break;
		case R.id.brand3:
			if (position != 0) {
				ArrayList<String> model = new ArrayList<String>();

				model.add(getString(R.string.select));
				model.add("model 1");
				model.add("model 1");
				model.add("model 1");
				model.add("model 1");

				ArrayAdapter<String> adapter_model = new ArrayAdapter<String>(
						context, android.R.layout.simple_spinner_item, model);
				spModel3.setAdapter(adapter_model);
				spModel3.setEnabled(true);
			} else {
				spModel3.setEnabled(false);
				spModel3.setSelection(0);
				image3.setImageResource(R.drawable.phone);
				
			}
			break;
		case R.id.model1:

			if (position != 0) {
				spBrand2.setEnabled(true);
				image1.setImageResource(R.drawable.phone2);
			} else {
				spBrand2.setEnabled(false);
				spBrand2.setSelection(0);
				spModel3.setEnabled(false);
				spModel3.setSelection(0);
				image1.setImageResource(R.drawable.phone);
				image2.setImageResource(R.drawable.phone);
				image3.setImageResource(R.drawable.phone);
			}
			break;
		case R.id.model2:
			if (position != 0) {
				spBrand3.setEnabled(true);
				image2.setImageResource(R.drawable.phone2);
			} else {
				spBrand3.setEnabled(false);
				spBrand3.setSelection(0);
				image3.setImageResource(R.drawable.phone);
			}

			break;
		case R.id.model3:
			if (position != 0) {
				image3.setImageResource(R.drawable.phone2);
			}else{
				image3.setImageResource(R.drawable.phone);
			}
			break;

		}

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btSend:
			if (validate()) {
				createTable(getListPhone());
				Toast t = Toast.makeText(context, "Validar ok", 3000);
				t.show();
			}
			break;
		case R.id.btClean:
			spBrand1.setSelection(0);
			spBrand2.setSelection(0);
			spBrand3.setSelection(0);
			
			spBrand1.setEnabled(true);
			spBrand2.setEnabled(false);
			spBrand3.setEnabled(false);
			
			spModel1.setSelection(0);
			spModel2.setSelection(0);
			spModel3.setSelection(0);
			
			spModel1.setEnabled(false);
			spModel2.setEnabled(false);
			spModel3.setEnabled(false);
			

//			spModel1.setBackgroundColor(R.drawable.list_item_bg_pressed);
//			spModel2.setBackgroundColor(getColor(2));
//			spModel3.setBackgroundColor(getColor(2));
			
			image1.setImageResource(R.drawable.phone);
			image2.setImageResource(R.drawable.phone);
			image3.setImageResource(R.drawable.phone);
			
			tableLayout.removeAllViews();
			break;

		default:
			break;
		}

	}

	private void createTable(ArrayList<Phone> listPhone) {

		ArrayList<String> categories = Tools.getCategories(context);

		tableLayout.removeAllViews();

		String titleCategory = categories.get(0);
		TextView category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));

		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		String text = null;
		TextView txtField = null;
		ImageView image = new ImageView(context);
		TableRow row = new TableRow(context);
		row.setBackgroundColor(getColor(1));
		row.addView(category);

		row.setBackgroundResource(R.drawable.shape_table_row_top);
		for (int i = 0; i < listPhone.size(); i++) {
			txtField = new TextView(context);
			text = "";
			if (listPhone.get(i).getBrand() != null
					&& !listPhone.get(i).getBrand().isEmpty()) {
				text = listPhone.get(i).getBrand();
			}
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);
		
		titleCategory = categories.get(1);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);
		

		row = new TableRow(context);
		row.setBackgroundColor(getColor(0));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {

			text = "";
			if (listPhone.get(i).getGama() != null
					&& !listPhone.get(i).getGama().isEmpty()) {
				text = listPhone.get(i).getGama();
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);
		
		
		

		titleCategory = categories.get(2);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);
		

		row = new TableRow(context);
		row.setBackgroundColor(getColor(1));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {

			text = "";
			if (listPhone.get(i).getModel() != null
					&& !listPhone.get(i).getModel().isEmpty()) {
				text = listPhone.get(i).getModel();
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(3);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(0));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getOperatingSystem() != null
					&& !listPhone.get(i).getOperatingSystem().isEmpty()) {
				text = listPhone.get(i).getOperatingSystem();
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(4);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(1));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getYear() != 0) {
				text = listPhone.get(i).getYear() + "";
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(5);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(0));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getNameCore() != null
					&& !listPhone.get(i).getNameCore().isEmpty()) {
				text = listPhone.get(i).getNameCore();
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(6);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(1));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getNumberCore() != 0) {
				text = listPhone.get(i).getNumberCore() + "";
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(7);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(0));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getSpeedCore() != 0) {
				text = listPhone.get(i).getSpeedCore() + "";
				if (listPhone.get(i).getSpeedDesCore() != null
						&& !listPhone.get(i).getSpeedDesCore().isEmpty()) {
					text += listPhone.get(i).getSpeedDesCore();
				}
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(8);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(1));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getMemoryROM() != 0) {
				text = listPhone.get(i).getMemoryROM() + "";
				if (listPhone.get(i).getMemoryDesRom() != null
						&& !listPhone.get(i).getMemoryDesRom().isEmpty()) {
					text += listPhone.get(i).getMemoryDesRom() + "";
				}
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(9);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(0));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getMemoryRAM() != 0) {
				text = listPhone.get(i).getMemoryRAM() + "";
				if (listPhone.get(i).getMemoryDesRam() != null
						&& !listPhone.get(i).getMemoryDesRam().isEmpty()) {
					text += listPhone.get(i).getMemoryDesRam() + "";
				}
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(10);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(1));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getBattery() != 0) {
				text = listPhone.get(i).getBattery() + "";
				if (listPhone.get(i).getBatteryDes() != null
						&& !listPhone.get(i).getBatteryDes().isEmpty()) {
					text += listPhone.get(i).getBatteryDes() + "";
				}
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(11);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(0));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			image = new ImageView(context);
			if (listPhone.get(i).isSdCard()) {
				image.setImageResource(R.drawable.check);
			} else {
				image.setImageResource(R.drawable.delete);
			}
			image.setBaselineAlignBottom(true);
			row.addView(image);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(12);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(1));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getBackCamera() != 0) {
				text = listPhone.get(i).getBackCamera() + "";
				if (listPhone.get(i).getCameraDes() != null) {
					text += listPhone.get(i).getCameraDes();
				}
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(13);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(0));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getFrontCamera() != 0) {
				text = listPhone.get(i).getFrontCamera() + "";
				if (listPhone.get(i).getCameraDes() != null) {
					text += listPhone.get(i).getCameraDes();
				}
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(14);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(1));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			image = new ImageView(context);
			if (listPhone.get(i).isFlash()) {
				image.setImageResource(R.drawable.check);
			} else {
				image.setImageResource(R.drawable.delete);
			}

			image.setBaselineAlignBottom(true);
			row.addView(image);
		}
		tableLayout.addView(row);

//		titleCategory = categories.get(14);
//		category = new TextView(context);
//		category.setText(titleCategory);
//		category.setTextColor(getColor(2));
//		category.setGravity(Gravity.LEFT);
//		category.setPadding(3, 0, 3, 0);
//
//		row = new TableRow(context);
//		row.setBackgroundColor(getColor(0));
//		row.addView(category);
//
//		for (int i = 0; i < listPhone.size(); i++) {
//			text = "";
//			if (listPhone.get(i).getFrecuency() != null
//					&& !listPhone.get(i).getFrecuency().isEmpty()) {
//				text = listPhone.get(i).getFrecuency();
//			}
//			txtField = new TextView(context);
//			txtField.setText(text);
//			txtField.setPadding(0, 0, 3, 0);
//			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
//			txtField.setTextColor(getColor(2));
//			txtField.setEllipsize(TruncateAt.START);
//
//			row.addView(txtField);
//		}
//		tableLayout.addView(row);

		titleCategory = categories.get(15);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(0));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getConectivity() != null
					&& !listPhone.get(i).getConectivity().isEmpty()) {
				text = listPhone.get(i).getConectivity();
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(17);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(1));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getNumberSim() != 0) {
				text = listPhone.get(i).getNumberSim() + "";
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(18);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(0));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getScreenSize() != 0) {
				text = listPhone.get(i).getScreenSize() + "''";
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(19);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(1));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getScreenTecnology() != null
					&& !listPhone.get(i).getScreenTecnology().isEmpty()) {
				text = listPhone.get(i).getScreenTecnology();
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(20);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(0));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getResolution() != null
					&& !listPhone.get(i).getResolution().isEmpty()) {
				text = listPhone.get(i).getResolution();
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(21);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(1));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {

			image = new ImageView(context);
			if (listPhone.get(i).isGps()) {
				image.setImageResource(R.drawable.check);
			} else {
				image.setImageResource(R.drawable.delete);
			}

			image.setBaselineAlignBottom(true);
			row.addView(image);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(22);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(0));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			image = new ImageView(context);
			if (listPhone.get(i).isWifi()) {
				image.setImageResource(R.drawable.check);
			} else {
				image.setImageResource(R.drawable.delete);
			}

			image.setBaselineAlignBottom(true);
			row.addView(image);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(23);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(1));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			image = new ImageView(context);
			if (listPhone.get(i).isBluetooth()) {
				image.setImageResource(R.drawable.check);
			} else {
				image.setImageResource(R.drawable.delete);
			}
			image.setBaselineAlignBottom(true);
			row.addView(image);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(24);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(0));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getDimensions() != null
					&& !listPhone.get(i).getDimensions().isEmpty()) {
				text = listPhone.get(i).getDimensions();
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(25);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(1));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getPrice() != 0) {
				text = listPhone.get(i).getPrice() + "";
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));

			row.addView(txtField);
		}
		tableLayout.addView(row);

		titleCategory = categories.get(26);
		category = new TextView(context);
		category.setText(titleCategory);
		category.setTextColor(getColor(2));
		category.setGravity(Gravity.LEFT);
		category.setPadding(3, 0, 3, 0);

		row = new TableRow(context);
		row.setBackgroundColor(getColor(0));
		row.addView(category);

		for (int i = 0; i < listPhone.size(); i++) {
			text = "";
			if (listPhone.get(i).getBechmark() != 0) {
				text = listPhone.get(i).getBechmark() + "";
			}
			txtField = new TextView(context);
			txtField.setText(text);
			txtField.setPadding(0, 0, 3, 0);
			txtField.setGravity(Gravity.CENTER_HORIZONTAL);
			txtField.setTextColor(getColor(2));
			row.addView(txtField);
		}
		row.setBackgroundResource(R.drawable.shape_table_row_bottom);
		tableLayout.addView(row);
	}

	private int getColor(int i) {
		int color = 0;
		switch (i) {
		case 0:
			color = Color.DKGRAY;
			break;
		case 1:
			color = Color.BLACK;
			break;
		case 2:
			color = Color.WHITE;
			break;
		default:
			break;
		}
		return color;
	}

	private boolean validate() {
		boolean valid = true;
		int con = 0;
		if (spBrand1.getSelectedItemPosition() != 0) {
			if (spModel1.getSelectedItemPosition() == 0) {
				valid = false;
				//spModel1.setBackgroundColor(getColor(0));
				

			} else {
				con++;
			}

		}

		if (spBrand2.getSelectedItemPosition() != 0) {
			if (spModel2.getSelectedItemPosition() == 0) {
				valid = false;
				//spModel2.setBackgroundColor(getColor(0));
			} else {
				con++;
			}
		}

		if (spBrand3.getSelectedItemPosition() != 0) {
			if (spModel3.getSelectedItemPosition() == 0) {
				valid = false;
				//spModel3.setBackgroundColor(getColor(0));
			} else {
				con++;
			}
		}
		if (con <= 1) {
			Toast t = Toast.makeText(context, "Selecione 2 o mas", 3000);
			t.show();
			valid = false;
		}
		return valid;
	}

	private ArrayList<Phone> getListPhone() {

		ArrayList<Phone> listPhone = new ArrayList<Phone>();

		Phone phone = new Phone();
		phone = new Phone();
		phone.setLogo(R.drawable.cubot);
		phone.setIdPhone(10);
		phone.setBrand("HTC");
		phone.setModel("C11");
		phone.setYear(2002);
		phone.setGama("Galaxy");
		phone.setNameCore("MTK657");
		phone.setSpeedCore(1.5);
		phone.setSpeedDesCore("Hz");
		phone.setScreenSize(5);
		phone.setMemoryROM(16.0);
		phone.setMemoryDesRom("MB");
		phone.setMemoryRAM(1.0);
		phone.setMemoryDesRam("GB");
		phone.setOperatingSystem("Android 4.2");
		//phone.setImage(R.drawable.img);
		phone.setBackCamera(14.4);
		phone.setFrontCamera(5);
		phone.setCameraDes("MP");
		phone.setScreenTecnology("HVGA");
		phone.setNumberSim(2);
		phone.setBattery(3600.0);
		phone.setBatteryDes("mAh");
		phone.setNumberCore(1);
		phone.setDimensions("25x45x71");
		phone.setBluetooth(false);
		phone.setWifi(true);
		phone.setConectivity("WCDMA");
		phone.setFrecuency("3G: 9000 2G 4000");
		phone.setGps(true);
		phone.setPrice(654D);
		phone.setResolution("356x1027");
		phone.setBechmark(180000D);
		listPhone.add(phone);

		phone = new Phone();
		phone.setLogo(R.drawable.cubot);
		phone.setIdPhone(4);
		phone.setBrand("Cubot");
		phone.setModel("C11");
		phone.setNameCore("MTK657");
		phone.setYear(2006);
		phone.setSpeedCore(1.5);
		phone.setSpeedDesCore("Hz");
		phone.setGama("Xperia");
		phone.setScreenSize(5);
		phone.setMemoryROM(16.0);
		phone.setNumberSim(1);
		phone.setMemoryDesRom("MB");
		phone.setMemoryRAM(3.0);
		phone.setBatteryDes("mAh");
		phone.setMemoryDesRam("GB");
		phone.setOperatingSystem("Android 4.2");
		//phone.setImage(R.drawable.img);
		phone.setBackCamera(14.4);
		phone.setFrontCamera(5);
		phone.setCameraDes("MP");
		phone.setBattery(3600.0);
		phone.setDimensions("25x45x71");
		phone.setBluetooth(false);
		phone.setWifi(true);
		phone.setConectivity("WCDMA");
		phone.setFrecuency("3G: 9000 2G 4000");
		phone.setGps(true);
		phone.setBechmark(120000D);
		phone.setNumberCore(8);
		phone.setResolution("480x1500");
		phone.setPrice(350D);
		phone.setScreenTecnology("HVGA");
		listPhone.add(phone);

		phone = new Phone();
		phone.setLogo(R.drawable.cubot);
		phone.setIdPhone(13);
		phone.setGama("Galaxy");
		phone.setBrand("Nokia");
		phone.setModel("C11");
		phone.setYear(2014);
		phone.setNameCore("MTK657");
		phone.setSpeedCore(1.5);
		phone.setScreenTecnology("GOS");
		phone.setSpeedDesCore("Hz");
		phone.setScreenSize(5);
		phone.setMemoryROM(1.6);
		phone.setMemoryDesRom("MB");
		phone.setMemoryRAM(256.0);
		phone.setMemoryDesRam("GB");
		phone.setOperatingSystem("Android 4.2");
		//phone.setImage(R.drawable.img);
		phone.setBackCamera(14.4);
		phone.setBatteryDes("mAh");
		phone.setFrontCamera(5);
		phone.setCameraDes("MP");
		phone.setBattery(3600.0);
		phone.setNumberSim(2);
		phone.setDimensions("25x45x71");
		phone.setBluetooth(false);
		phone.setWifi(true);
		phone.setConectivity("WCDMA");
		phone.setFrecuency("3G: 9000 2G 4000");
		phone.setGps(true);
		phone.setPrice(225D);
		phone.setNumberCore(4);
		phone.setBechmark(210000D);
		phone.setResolution("480x320");
		listPhone.add(phone);

		return listPhone;
	}
}
